﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Ocmf.Forms;

namespace Ocmf.Types
{
    public class Stop:Action
    {
        private bool isConditional;
        public bool IsConditional
        {
            get
            {
                return isConditional;
            }
            set
            {
                isConditional = value;
                Text = (isConditional ? "Stop if " + counter + " is " + Enum.GetName(typeof(Inequality), Inequality) + " to " + conditionalValue : "Stop");
            }
        }

        private string counter;
        public string Counter
        {
            get
            {
                return counter;
            }
            set
            {
                counter = value;
                Text = (isConditional ? "Stop if " + counter + " is " + Enum.GetName(typeof(Inequality), Inequality) + " to " + conditionalValue : "Stop");
            }
        }

        private Inequality inequality;
        public Inequality Inequality
        {
            get
            {
                return inequality;
            }
            set
            {
                inequality = value;
                Text = (isConditional ? "Stop if " + counter + " is " + Enum.GetName(typeof(Inequality), Inequality) + " to " + conditionalValue : "Stop");
            }
        }

        private int conditionalValue;
        public int ConditionalValue
        {
            get
            {
                return conditionalValue;
            }
            set
            {
                conditionalValue = value;
                Text = (isConditional ? "Stop if " + counter + " is " + Enum.GetName(typeof(Inequality), Inequality) + " to " + conditionalValue : "Stop");
            }
        }

        public Stop()
        {
            Text = (isConditional ? "Stop if " + counter + " is " + Enum.GetName(typeof(Inequality), Inequality) + " to " + conditionalValue : "Stop");
            SelectedImageKey = ImageKey = "stop";
        }

        public override object Clone()
        {
            var clone = base.Clone();
            return clone;
        }

        public override System.Windows.Forms.Form Editor
        {
            get { return new EditStopForm(this); }
        }

        public override XmlElement ToXml(XmlDocument xmlDocument)
        {
            //create trial xml element
            var stopElement = xmlDocument.CreateElement("Stop");

            var isConditionalAttribute = xmlDocument.CreateAttribute("IsConditional");
            isConditionalAttribute.InnerText = isConditional.ToString();
            stopElement.Attributes.Append(isConditionalAttribute);

            if(isConditional)
            {
                var counterElement = xmlDocument.CreateElement("Counter");
                counterElement.InnerText = counter;
                stopElement.AppendChild(counterElement);

                var inequalityElement = xmlDocument.CreateElement("Inequality");
                inequalityElement.InnerText = Enum.GetName(typeof (Inequality), inequality);
                stopElement.AppendChild(inequalityElement);

                var conditionalValueElement = xmlDocument.CreateElement("ConditionalValue");
                conditionalValueElement.InnerText = conditionalValue.ToString();
                stopElement.AppendChild(conditionalValueElement);
            }

            //return element
            return stopElement;
        }

        public override void FromXml(XmlNode xmlElement)
        {
            isConditional = bool.Parse(xmlElement.Attributes["IsConditional"].InnerText);

            if (!isConditional) return;

            Counter = xmlElement["Counter"].InnerText;
            Inequality = (Inequality) Enum.Parse(typeof (Inequality), xmlElement["Inequality"].InnerText);
            ConditionalValue = Int32.Parse(xmlElement["ConditionalValue"].InnerText);
        }
    }
}
